﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Media.Imaging;

using Game.Common;
using Game.Common.Resources;
using Game.ExtensibleObjects;
using Game.Map;
using Game.Types;

namespace Game.Toolkit
{
    public class GameObjectGroup
    {
        readonly ReadOnlyDictionary<GameDirection, BitmapImage> _Dictionary;
        readonly string _Name;
        readonly BuildingType _BuildingType;

        internal GameObjectGroup(GameObjectGroupBuilder builder)
        {
            _Dictionary = new ReadOnlyDictionary<GameDirection, BitmapImage>(builder.Dictionary);
            _Name = builder.Name;
            _BuildingType = builder.BuildingType;
        }
        
        public string Name
        {
            get { return _Name; }
        }
        
        public BuildingType BuildingType
        {
            get { return _BuildingType; }
        }
        
        public List<GameObjectGroupPart> Parts
        {
            get
            {
                var list = new List<GameObjectGroupPart>();
                foreach(var direction in _Dictionary.Keys)
                {
                    var image = _Dictionary[direction];
                    var part = new GameObjectGroupPart(
                        direction, _Name, image);
                    list.Add(part);
                }
                
                return list;
            }
        }
        
    }
    
    class GameObjectGroupBuilder
    {
        readonly string _Name;
        readonly Dictionary<GameDirection, BitmapImage> _Dictionary;

        BuildingType _BuildingType;

        public GameObjectGroupBuilder(
            string name, 
            IGameExtension gameAbility = null,
            IRuntimeExtensionFactory runtimeX = null,
            GameResources resources = null)
        {
            _Dictionary = new Dictionary<GameDirection, BitmapImage>();
            _Name = name;

            var b = new BuildingTypeBuilder {Name = name};
            if (resources == null)
            {
                b.Tank = new GameResources(0, 20, 0);
            }
            else
            {
                b.Tank = resources;
            }

            if (gameAbility != null)
            {
                b.Extensions.Add(gameAbility);
            }
            if (runtimeX != null)
            {
                b.RuntimeExtensionFactories.Add(runtimeX);
            }

            _BuildingType = b.BuildBuildingType();
        }
        
        public BuildingType BuildingType
        {
            get { return _BuildingType; }
            set { _BuildingType=value; }
        }

        public string Name
        {
            get { return _Name; }
        }

        public Dictionary<GameDirection, BitmapImage> Dictionary
        {
            get { return _Dictionary; }
        }

        public void Add(GameDirection direction, BitmapImage image)
        {
            _Dictionary.Add(direction, image);
        }

        public void Add(BitmapImage image, ObjectOrientation objectOrientation)
        {
            GameDirection direction;
            switch(objectOrientation)
            {
                case ObjectOrientation.Center:
                    direction = new GameDirection(1, 0);
                    break;
                case ObjectOrientation.North:
                    direction = new GameDirection(1, -1);
                    break;
                case ObjectOrientation.South:
                    direction = new GameDirection(1, 1);
                    break;
                case ObjectOrientation.Northeast:
                    direction = new GameDirection(2, 0);
                    break;
                case ObjectOrientation.Northwest:
                    direction = new GameDirection(0, 0);
                    break;
                case ObjectOrientation.Southeast:
                    direction = new GameDirection(2, 1);
                    break;
                case ObjectOrientation.Southwest:
                    direction = new GameDirection(0, 1);
                    break;
                default:
                    Debugger.Break();
                    direction = new GameDirection(0, 0);
                    break;
            }
            
            _Dictionary.Add(direction, image);
        }
        
        internal GameObjectGroup Build()
        {
            return new GameObjectGroup(this);
        }
    }
}
